package org.despair2D.ui.puppet 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import org.despair2D.resource.ILoader;
	import org.despair2D.debug.Logger;
	import org.despair2D.renderer.IView;
	import org.despair2D.resource.LoaderManager;
	import org.despair2D.ui.core.Component;
	
	import org.despair2D.core.ns_despair;
	use namespace ns_despair;
	
	
public class ImagePuppet extends PuppetProxy 
{
	
	public function ImagePuppet() 
	{
		m_view = new ImagePuppetComp(this);
	}
	
	
	
	/* 是否为空画像 */
	final public function get isEmpty() : Boolean
	{
		return Boolean(m_key == null)
	}
	
	
	/**
	 * 嵌入
	 * @param	bitmapRef
	 * @param	whetherCached
	 */
	public function embed( bitmapRef:Class, whetherCached:Boolean = true ) : void
	{
		var key:String
		var bmp:Bitmap
		var bmd:BitmapData
		
		this._checkAndStopLoad()
		
		key  =  getQualifiedClassName(bitmapRef)
		bmd  =  m_bitmapDataList[key]
		
		if (!bmd)
		{
			try
			{
				bmp = new bitmapRef()
			}
			
			catch (error:Error)
			{
				Logger.reportError(this, 'embed', '参数定义类型错误...!')
			}
			
			bmd = bmp.bitmapData
			if (whetherCached)
			{
				m_bitmapDataList[key] = bmd
			}
		}
		
		if (!this.isEmpty && !m_bitmapDataList[m_key])
		{
			m_view.m_img.bitmapData.dispose()
		}
		
		m_view.m_img.bitmapData  =  bmd
		m_key                    =  key
		
		this._adjustAlign()
	}
	

	
	/**
	 * 载入画像
	 * @param	url
	 * @param	whetherCached
	 * @param	optional
	 */
	public function load( url:String, whetherCached:Boolean = true, optional:Class = null ) : ILoader
	{
		var bmd:BitmapData
		
		this._checkAndStopLoad()
		bmd = m_bitmapDataList[url]
		if (bmd)
		{
			if (!this.isEmpty && !m_bitmapDataList[m_key])
			{
				m_view.m_img.bitmapData.dispose()
			}
			
			m_view.m_img.bitmapData  =  bmd
			m_key                    =  url
			
			this._adjustAlign()
			return null
		}
		
		// 预载画像
		if(optional)
		{
			this.embed(optional)
		}
		
		m_whetherCached  =  whetherCached
		m_loader         =  LoaderManager.getInstance().getLoader(url).addCompleteListener(_onComplete).addFailListener(_onFail)
		return m_loader
	}
	
	
	/**
	 * 削除画像
	 */
	final public function empty() : void
	{
		this._checkAndStopLoad()
		if (!this.isEmpty && !m_bitmapDataList[m_key])
		{
			m_view.m_img.bitmapData.dispose()
		}
		
		m_view.m_img.bitmapData  =  null
		m_key                    =  null
	}
	
	
	
	final ns_despair function _onComplete( e:Event ) : void
	{
		var bmd:BitmapData
		
		bmd = (m_loader.data as Bitmap).bitmapData
		
		// 削除缓存
		if (!this.isEmpty && !m_bitmapDataList[m_key])
		{
			m_view.m_img.bitmapData.dispose()
		}
		
		if (m_whetherCached)
		{
			m_bitmapDataList[m_loader.url] = bmd
		}
		
		m_view.m_img.bitmapData  =  bmd
		m_key                    =  m_loader.url
		m_loader                 =  null
		
		this._adjustAlign()
	}
	
	
	final ns_despair function _onFail( e:IOErrorEvent ) : void
	{
		Logger.reportWarning(this, '_onFail', 'IO Error(ImagePuppet): ' + m_loader.url)
		m_loader = null
	}
	
	
	final ns_despair function _checkAndStopLoad() : void
	{
		if (m_loader)
		{
			m_loader.removeCompleteListener(_onComplete)
			m_loader.removeFailListener(_onFail)
			m_loader = null
		}
	}
	
	
	final ns_despair function _adjustAlign() : void
	{	
		if (m_isCenter)
		{
			m_view.m_img.x = -m_view.width  * 0.5;
			m_view.m_img.y = -m_view.height * 0.5
		}
	}
	
	
	final override ns_despair function get view() : IView
	{
		return m_view;
	}
	
		
	final override ns_despair function recycle() : void
	{
		this.empty()
		super.recycle();
		
		cachedImagePuppetList[cachedImagePuppetLength++] = this;
	}
	
	
	ns_despair static function getImagePuppet( isCenter:Boolean ) : ImagePuppet
	{
		var image:ImagePuppet = Boolean(cachedImagePuppetLength > 0 ? cachedImagePuppetLength-- : 0) ? cachedImagePuppetList.pop() : new ImagePuppet();
		image.m_isCenter = isCenter
		return image;
	}
	
	
	
	ns_despair static var cachedImagePuppetList:Array = []
	
	ns_despair static var cachedImagePuppetLength:int;
	
	ns_despair static var m_bitmapDataList:Object = new Object() // 画像列表
	
	
	ns_despair var m_view:ImagePuppetComp
	
	ns_despair var m_key:String
	
	ns_despair var m_loader:ILoader
	
	ns_despair var m_isCenter:Boolean
		
	ns_despair var m_whetherCached:Boolean

}

}